package org.mbds.android.sismapp.activity;

import java.util.ArrayList;

import org.mbds.android.location.GPSTools;
import org.mbds.android.sismapp.SismappMenu;
import org.mbds.android.sismapp.R;
import org.mbds.android.sismapp.SismappMessage;
import org.mbds.android.sismapp.SismappStaticCommons;
import org.mbds.android.sismapp.SismappUser;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class GoogleMapActivity extends Activity implements LocationListener {
	private GoogleMap map;
	private ArrayList<Marker> networkUsers = new ArrayList<Marker>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_googlemap);
		map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_view)).getMap();
		map.setMyLocationEnabled(true);
		onLocationChanged(GPSTools.getLastKnownPosition());
		updateNetworkUsers();
	}
	
	private void updateNetworkUsers() {
		networkUsers.clear();
		try {
			SismappMessage.sendSismappMessage(SismappMessage.MESSAGE_USER_SISMAPP_CONNECTED);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i=0; i<SismappStaticCommons.getNetworkUsers().size();i++) {
			SismappUser user = SismappStaticCommons.getNetworkUsers().get(i);
			LatLng loc = new LatLng(user.getLocation().getLatitude(),user.getLocation().getLongitude());
			Marker m = map.addMarker(new MarkerOptions().position(loc)
					.title(user.getFirstName()+" "+user.getLastName())
					.snippet(GPSTools.getAddress(user.getLocation(), this)));
			networkUsers.add(m);
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		GPSTools.addLocationListenerGPS(this);
	}

	@Override
	public void finish() {
		GPSTools.removeLocationListenerGPS(this);
		super.finish();
	}
	
	/***********************/
	/* Create options menu */
	/***********************/
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		SismappMenu.onCreateOptionsMenu(this, menu);
		return super.onCreateOptionsMenu(menu);
	}

	/************************/
	/* Prepare options menu */
	/************************/
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		SismappMenu.onPrepareOptionsMenu(this, menu);
		return super.onPrepareOptionsMenu(menu);
	}

	/*******************************/
	/* Manage menu item selection */
	/*******************************/
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		SismappMenu.onOptionsItemSelected(this, item);
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onLocationChanged(Location loc) {
		if (loc!=null) {
			LatLng userLocation = new LatLng(loc.getLatitude(),loc.getLongitude());
			Marker user = map.addMarker(new MarkerOptions().position(userLocation)
					.title(SismappStaticCommons.getFirstName()+" "+SismappStaticCommons.getLastName())
					.snippet(GPSTools.getAddress(loc, this))
					.icon(BitmapDescriptorFactory
							.fromResource(R.drawable.ic_launcher)));
	
			// Move the camera instantly to hamburg with a zoom of 15.
			map.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
			// Also update sismapp network users if found
			updateNetworkUsers();
		}
	}
	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}
